#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
       /*打开文件*/
   const char *filename = "./record.txt";
   int fd = open(filename, O_RDWR | O_CREAT,0644);
   if(fd == -1)
   {
      perror("open error");
      exit(-1);
   }
    printf("fd = %d\n",fd);
    /*从文件开头向后移动20字节*/
    off_t offset = lseek(fd,50,SEEK_SET);
    printf("offset = %ld\n",offset);
    char buffer[128] = {0};
    read(fd,buffer,sizeof(buffer) -1);
    printf("buffer = %s\n",buffer);
    /*可通过lseek函数移动文件指针到文件末尾,计算文件长度*/
    offset = lseek(fd,0,SEEK_END);
    printf("offset = %ld\n",offset);
    /*关闭文件*/
    close(fd);
    return 0;
    }